package com.example.demo.init;

import com.example.demo.annotation.OrderOperator;
import com.example.demo.annotation.OrderProcessor;
import com.example.demo.manager.OrderStatusManager;
import com.example.demo.operator.AbstractOrderOperator;
import com.example.demo.processor.AbstractOrderProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author zhy
 * @version 1.0.0
 * @ClassName Initialization.java
 * @Description TODO
 * @createTime 2020年12月02日 16:06:00
 */
@Component
public class Initialization implements BeanPostProcessor {

    private Logger logger = LoggerFactory.getLogger(Initialization.class);

    @Autowired
    private OrderStatusManager orderStatusManager;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof AbstractOrderOperator && bean.getClass().isAnnotationPresent(OrderOperator.class)) {
            AbstractOrderOperator orderOperator = (AbstractOrderOperator) bean;
            orderStatusManager.orderOperatorMaps.put(orderOperator.getStatus(), orderOperator);
        }
        if (bean instanceof AbstractOrderProcessor && bean.getClass().isAnnotationPresent(OrderProcessor.class)) {
            AbstractOrderProcessor orderProcessor = (AbstractOrderProcessor) bean;
            orderStatusManager.orderProcessorMaps.put(orderProcessor.getStatus(), orderProcessor);
        }
        return bean;
    }
}
